from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb


class BitlistpageSpider(BaseSpider):

    name = "bitlistpage"
    baseurl ="http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/"

    conn = MySQLdb.connect(host="localhost",user="spider",passwd="Xjk@4047218?",db="spider")
    cursor = conn.cursor()
    sql = "select url from sp_ba_carmodel order by id asc"
    result = cursor.execute(sql)
    
    carmodel_list_url = []
    for row in cursor.fetchall():
        url =  row[0]+"baojia/c0/?bizmode=1"
        carmodel_list_url.append(url)
    cursor.close()
    #start_urls = ["http://car.bitauto.com/aodia3-3999/m104780/baojia/c0/?bizmode=1"]
    start_urls = carmodel_list_url 


    def parse(self,response):
        
        sel = Selector(response)
        item = SpiderItem()   
        item["c1"] = response.url.split("/")[-4]        
        area = sel.xpath('//div[@id="cars_pager"]')
        item["page"] = area.xpath('.//a[last()-1]/text()').extract()
        item["type"] = "bitlistpage"

        return item
